package com.suse.warmstill.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.warmstill.common.Datas;
import com.suse.warmstill.common.enums.MqQueueConstants;
import com.suse.warmstill.common.enums.RedisEnum;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.common.utils.UserIdThreadLocal;
import com.suse.warmstill.entity.ChatMsg;
import com.suse.warmstill.entity.TbUserinfo;
import com.suse.warmstill.service.ChatMsgService;
import com.suse.warmstill.mapper.ChatMsgMapper;
import com.suse.warmstill.service.UserInfoService;
import com.suse.warmstill.vo.MessageIndexVo;
import com.suse.warmstill.vo.MessageInfo;
import com.suse.warmstill.vo.MqMessage;
import com.suse.warmstill.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ADMIN
 * @description 针对表【chat_msg】的数据库操作Service实现
 * @createDate 2023-02-12 05:23:07
 */
@Service
@Slf4j
public class ChatMsgServiceImpl extends ServiceImpl<ChatMsgMapper, ChatMsg>
        implements ChatMsgService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;


    @Resource
    private ChatMsgMapper msgMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private UserInfoService userInfoService;


    @Override
    public CallResult<ListPageModel<MessageIndexVo>> getUnRead(Long accepterId) {
        //返回 用户列表 id，username，头像，最近的一条信息,未接收消息的数量
        final LambdaQueryWrapper<ChatMsg> msgWrapper = new LambdaQueryWrapper<>();
        msgWrapper.eq(ChatMsg::getAcceptUserId, accepterId);
        msgMapper.selectList(msgWrapper).stream().map(ChatMsg::getSendUserId);


        return null;
    }


    @Override
    public CallResult<ListPageModel<ChatMsg>> getHistoryMessage(long acceptId, int currentPage, int pageSize) {

        Long sendId = UserIdThreadLocal.get();
        //      Long sendId =111L;
        String roomId = sendId > acceptId ? String.valueOf(sendId) + String.valueOf(acceptId) : String.valueOf(acceptId) + String.valueOf(sendId);

        String chatList = redisTemplate.opsForValue().get(RedisEnum.CHAT_ROOM + roomId);

        if (ObjectUtils.isEmpty(chatList)) {
            final ListPageModel<ChatMsg> model = new ListPageModel<>();
            model.setPageCount(0);
            model.setPageSize(0);
            model.setCurrentPage(0);
            model.setDataSize(0);
            model.setList(null);
            return CallResult.success(model);
        }
        List<ChatMsg> list = JSON.parseArray(chatList, ChatMsg.class);


        ListPageModel<ChatMsg> model = new ListPageModel<>();
        model.setPageCount(list.size() / pageSize);
        model.setDataSize(list.size());
        model.setPageSize(pageSize);
        model.setCurrentPage(currentPage);

        model.setList(list.subList(pageSize * currentPage, pageSize * currentPage + pageSize));


        return CallResult.success(model);


    }


    @Override
    public void sendMessage(Long acceptId, Long sendId, MessageInfo messageInfo) {


    }


    @Override
    public CallResult connect(int acceptId, int senderId) {

        String roomId = senderId > acceptId ? String.valueOf(senderId) + String.valueOf(acceptId) : String.valueOf(acceptId) + String.valueOf(senderId);

        TbUserinfo acceptUser = userInfoService.getUserInfo((long) acceptId);
        TbUserinfo sendUser = userInfoService.getUserInfo((long) senderId);
        HashMap<String, TbUserinfo> map = new HashMap<>();
        map.put("acceptUser", acceptUser);
        map.put("sendUser", sendUser);

        simpMessagingTemplate.convertAndSend("/websocket/connect/" + sendUser + "/+" + acceptUser, "map"
        );


        System.out.println("返回的map" + map);
        return CallResult.success(map);
    }

    @Override
    public void saveSessionUser(String session, UserInfo userInfo) {
        Datas.sessionUserInfo.put(session, userInfo);
    }

    @Override
    public void deleteSessionUser(String session) {
        UserInfo remove = Datas.sessionUserInfo.remove(session);
        if (!ObjectUtils.isEmpty(remove)) {
            log.info("删除了 session----->{},userInfo------->", session, remove);
        }
    }
}




